define(function(require, exports){
	var pubjs = require('pubjs');
	var view  = require('@base/view');
	var $ = require('jquery');
	var util = require('util');

	// 订单列表模块
	var List = view.container.extend({
		init: function( config ) {
			config = pubjs.conf( config, {
				'view_model': {
					'select': false
				}
			});
			this.Super( 'init', arguments );
		}
		,afterBuild: function() {
			this.append($([
				'<div class="P-dealCampaignDeal">',
					'<div class="operate">',
						'<label class=""><input type="checkbox" ms-duplex-radio="select">' + LANG('显示已选') + '</label>',
					'</div>',
				'<div class="porgrid" pub-name="grid" pub-mod="campaign/grid/deal.list"></div>',
				'</div>'
			].join('')));

			this.vm.watch('select', this.vmSelect);

			var c = this.getConfig();
			var config = {
				'grid': {
					'param': c.param,
					'hasSearch': true,
					'list':{
						'rowSelect': true,
						'scroll_type': 'row',
						'scroll_size': 8
					}
				}
			};
			this.buildFromTemplate(config);

		}
		,setParam: function() {
			this.$.grid.setParam.apply(this.$.grid, arguments);
			return this;
		}
		,load: function() {
			this.$.grid.load.apply(this.$.grid, arguments);
			return this;
		}
		,vmSelect: function() {
			var ids;
			if(this.vm.$.select){
				ids = this.$.grid.getSelectRowIds();
			}

			this.$.grid.setParam({ids: ids}, true);
			this.$.grid.reload();
		}
		,getValue: function() {
			return this.$.grid.getSelectRowIds()
		}
		,setValue: function(data) {
			this.$.grid.setSelectRowIds(data || []);
			return this;
		}
		,onChangeSelect:function(ev){
			this.fire('dealIdsChange', ev.param.selected);
			return false;
		}
		,reset: function() {
			this.vm.reset();
			this.$.grid.setSelectRowIds([]);
			return this;
		}
	});

	exports.list = List;

	var Spot = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'url': 'deal/spots'
				,'reqType': 'websocket'
				,'readonly': false
				,'view_model': {
					'selected': false
					,operator : [
						{'name': LANG('清空'), 'id': 'clear'}
						,{'name': LANG('全选所有'), 'id': 'all'}
					]
					,value: []
					,delEvent: this.vmRemoveItem.bind(this)
					,operateEvent: this.vmOperateEvent.bind(this)
				}
			});

			this.$ids = '';	//保存已经选择的广告位id集合.
			this.$param = {Status: 1};

			this.Super('init', arguments);
		}
		,afterBuild: function() {
			var self = this;
			var c = self.getConfig();
			self.append($([
				'<div class="P-adsGroupEditAds mb10">'
				,'<div pub-name="grid" pub-mod="campaign/grid/deal.spot"/>'
				,'<div >'
				,'<div class="selectOperate pt20">'
				,'<span class="title">'+ LANG('已选择广告位') +'</span>'
				,'<button class="btn btn-default mr5 fr" ms-repeat-op="operator" ms-click="operateEvent(op.id)">{{op.name}}</button>'
				,'</div>'
				,'<div class="selectContainer">'
				,'<ul ms-repeat-sel="value">'
				,'<li  ms-click="delEvent($index)" title="'+ LANG('点击移除广告位') +'">{{sel.Name}}</li>'
				,'</ul>'
				,'<div class="empty" ms-visible="value.length == 0">'+ LANG('请至少选择一个广告位') +'</div>'
				,'</div>'
				,'</div>'
				,'</div>'
			].join('')));

			self.vm.watch('selected', self.vmSelectEvent);

			var config = {
				'search': {

				}
				,'grid': {
					param: c.param
				}
			};

			self.buildFromTemplate(config);

		}
		,vmRemoveItem: function(index) {
			var value = this.vm.$.value;
			value.splice(index, 1);
			var ids = [];
			util.each(value, function(v) {
				ids.push(v._id);
			});

			this.$.grid.setSelectRowIds(ids);
			this.$ids = ids.join();
		}
		,vmSelectEvent: function() {
			var grid = this.$.grid;
			var select = this.vm.$.selected;
			grid.setPage(1);
			grid.setParam({'ids': select ? this.$ids: ''}).load();
		}
		,vmOperateEvent: function(type) {
			switch (type) {
				case 'clear':
					this.vm.setItem('value', []);
					this.$.grid.setSelectRowIds([]);
					this.$ids = '';
					break;
				case 'all':
					this.allSelect();
					break;
			}
		}
		,onChangeSelect: function(ev){
			var self = this;
			self.$ids = ev.param.selected;
			var allData = this.$.grid.getData();
			var data = [];
			util.each(allData, function(item) {
				if(self.$ids.indexOf(item.key) > -1) {
					data.push(item);
				}
			});

			self.vm.setItem('value', data);
		}
		,allSelect: function() {
			var self = this;
			pubjs.confirm(LANG('确认要选所有的广告位吗?'), function(res) {
				if(res) {
					self.vm.setItem('value', self.$.grid.getData());
				}
			});
		}
		,setParam: function(param, replace) {
			util.extend(this.$param, param);
			this.$.grid.setParam(param, replace);
			return this;
		}
		,load: function() {
			this.$.grid.load();
			return this;
		}
		,setValue: function(value) {
			var ids = [];
			if(value && value.length) {
				util.each(value, function(item) {
					ids.push(item.key);
				});
			}

			this.$.grid.setSelectRowIds(ids);
			this.$ids = ids.join();
			this.vm.setItem('value', value);
		}
		,getValue: function() {
			var data = this.vm.getItem('value');
			var tmp = {};
			util.each(data, function(item) {
				var key = item.key.split('_');
				var dealId = key.shift();
				if(!tmp[dealId]) {
					tmp[dealId] = [];
				}

				tmp[dealId].push(item._id);
			});

			return tmp;
		}
		,reset: function() {
			this.$param = {};
			this.vm.setItem('value', []);
			this.$.grid.setSelectRowIds([]);
			this.$ids = '';
			return this;
		}
	});
	exports.spot = Spot;
});